<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Netty-Websocket</title>
    <script type="text/javascript">
        let socket;
        if(!window.WebSocket){
            window.WebSocket = window.MozWebSocket;
        }
        const userId = new Date().getTime();
        if(window.WebSocket){
            socket = new WebSocket("ws://127.0.0.1:18888/ws/?userId=" + userId );
            socket.onmessage = function(event){
                const ta = document.getElementById('responseText');
                // var uidValue = document.getElementById('uid');
                // var msg = event.data;
                // var uid = msg.toString().split(":")[0];
                // var msgData = msg.toString().split(":")[1]
                const msgData = event.data;
                const msg = JSON.parse(msgData);
                ta.value += msg.data +"\r\n";
                let mp3 = new Audio('./mp3/msn.mp3') // 创建音频对象
                mp3.loop = false
                mp3.play()
            };
            socket.onopen = function(event){
                const ta = document.getElementById('responseText');
                ta.value = "Netty-WebSocket服务器。。。。。。连接  \r\n";
                const userIdEl = document.getElementById('userId');
                userIdEl.innerText = userId;
            };
            socket.onclose = function(event){
                const ta = document.getElementById('responseText');
                ta.value = "Netty-WebSocket服务器。。。。。。关闭 \r\n";
            };
        }else{
            alert("您的浏览器不支持WebSocket协议！");
        }
        let count = 1;
        function send(message,targetId){
            if(!window.WebSocket){return;}
            if(socket.readyState === WebSocket.OPEN){
                const tag = count === 1 ? 1 : 2;
                let msg = {
                    tag:tag,
                    userId:userId,
                    targetUserId:targetId,
                    data:message
                };
                console.log('msg',JSON.stringify(msg));
                socket.send(JSON.stringify(msg));
            }else{
                alert("WebSocket 连接没有建立成功！");
            }

        }

    </script>
</head>
<body>
<form onSubmit="return false;">
    <label>当前用户ID: </label><span id="userId"></span> <br />
    <label>目标用户ID:</label><input type="text" name="targetId"  id="targetId" /> <br />
    <label>消息内容:</label><input type="text"  style="width: 80%" name="message" /> <br />
   <input type="button" value="发送ws消息"
                  onClick="send(this.form.message.value,this.form.targetId.value)" />
    <hr color="black" />
    <h3>服务端返回的应答消息</h3>
    <textarea id="responseText" style="width: 1024px;height: 300px;"></textarea>
</form>
</body>
</html>
